<?php
/**
 * Created by b17
 * 2/23/12 6:21 PM
 */

/**
 * Портированный код Виталика
 */
class Admin_PollController extends G4T_AdminController
{
    public function __call($name, $args)
    {
        $this->_forward('manage');
    }

    public function manageAction()
    {
        $pollManager = new \Managers\PollVotingHistory();
        $polls = $pollManager->getQuestions();
        $this->view->polls = $polls;
    }

    public function addAction()
    {
        $pollManager = new \Managers\PollVotingHistory();

        $question = $this->_getParam('question');
        $answers = $this->_getParam('answer');
        $isactive = $this->_getParam('isactive');

        if(!$isactive)
        {
            $isactive =0;
        }

        if($question){
            $polls = $pollManager->addQuestion($question, $isactive);
            $id = $polls->getId();
            if($polls){
                $answer = $pollManager->addAnswers($id, $answers);
                $this->_flashMessenger->addMessage('Added successfully.', Lib_Flash::NOTICE, Lib_Flash::PRE);

                $this->_redirect('/admin/poll/manage');
            }
        }

    }

    public function editingAction()
    {
        $pollManager = new \Managers\PollVotingHistory();

        $id = $this->_getParam('id');
        if($id){
            $poll = $pollManager->getQuestion($id);

            $answers = $pollManager->getAnswersByPoolId($id);

        }
        $question = $this->_getParam('question');
        $answer = $this->_getParam('answer');

        if($question){

            if ($answer) {
                $isactive = $this->_getParam('isactive');

                If($isactive){
                    $polls = $pollManager->editQuestion($id,$question,1);
                }else{
                    $polls = $pollManager->editQuestion($id,$question,0);
                }
                $poll = $pollManager->editAnswer($answer);
                $new_answer = $this->_getParam('new_answer');
                if ($new_answer) {
                    $pollManager->addAnswers($id, $new_answer);
                }

            }
            $this->_flashMessenger->addMessage('Edited successfully.', Lib_Flash::NOTICE, Lib_Flash::PRE);
            $this->_redirect('/admin/poll/manage');
        }

        $this->view->poll = $poll;
        $this->view->answers = $answers;



    }

    public function deleteAction()
    {
        $pollManager = new \Managers\PollVotingHistory();

        $id = $this->_getParam('id');
        $ans_id = $this->_getParam('ans_id');
        if($id){
            $polls = $pollManager->delQuestion($id);
            $this->_flashMessenger->addMessage('Deleted successfully.', Lib_Flash::NOTICE, Lib_Flash::PRE);
            $this->_redirect('/admin/poll/manage');
        }
        if($ans_id){
            $polls = $pollManager->delAnswer($ans_id);
            $id = $pollManager->getPoolIdByAnswersId($ans_id);
            $this->_flashMessenger->addMessage('Deleted successfully.', Lib_Flash::NOTICE, Lib_Flash::PRE);
            $this->_redirect('/admin/poll/poll/id/'.$id['question']['id']);
        }

    }

    public function pollAction()
    {
        $pollManager = new \Managers\PollVotingHistory();
        $id = $this->_getParam('id');
        if($id){
            $poll = $pollManager->getQuestion($id);
            $answers = $pollManager->getAnswers($id);

            $name = $poll['name'];
        }

        $answer = $this->_getParam('answer');

        if($id){
            if($answer){
                $new_answer = $pollManager->addAnswers($id, $answer);
                $this->_flashMessenger->addMessage('Added successfully.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                $this->_redirect('/admin/poll/poll/id/'.$id);
            }
        }


        $this->view->name = $name;
        $this->view->answers = $answers;
        $this->view->id = $id;
    }


}
